<?php

namespace App\Http\Controllers\Home;

use GuzzleHttp\Client;
use App\Models\Phonemap;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class MyphoneController extends Controller
{
    protected $accountSid;
    protected $appid;
    protected $key;
    protected $type;
    protected $op;
    protected $baseurl;

    function __construct()
    {
        $this->accountSid = '8a216da86bfdbebb016c83d1f2c801bb';
        $this->appid = '8a216da86bfdbebb016c83d1f2e701bc';
        $this->key = '2d778297afd5406eb533fa3be55cdaf0';
        $this->type = 'axb';
        $this->op = '';
        $this->baseurl = 'https://apppro.cloopen.com:8883/2013-12-26';
    }

    /**
     * 供应商发布活动页面
     * @param url home.activity.actAdd
     * @param Request $request
     * @return bool
     */
    public function phoneMap(Request $request)
    {
        $phoneA = $request->post('phoneA');
        $phoneB = $request->post('phoneB');
        if (empty($phoneB)||empty($phoneA)) return response()->json(["code"=>0, "msg"=>'参数错误']);

        $client = new Client();
        $mothed = 'POST';
        $mapTime = 60 * 10;
        $timestamp = time();

        //是否有最近做过这两个号码到映射
        $map_info = Phonemap::where('phoneA',$phoneA)->where('phoneB',$phoneB)->where('status',1)->first();
        if($map_info['timestamp']>$timestamp){
            return $this->writeJson(1, $map_info['xnumber'], '申请电话隐私保护成功!');
        }
        $auth = "$this->accountSid:" . date("YmdHis", $timestamp);
//        dump('Authorization'.$auth);
        $auth = base64_encode($auth);
        $sigParameter = $this->accountSid . $this->key . date("YmdHis", $timestamp);
//        dump('sigParameter'.$sigParameter);
        $sigParameter = md5($sigParameter);
        $baseUrl = $this->baseurl . "/Accounts/$this->accountSid/nme/$this->type/cu01/providenumber?sig=$sigParameter";
        $data = [
            'appId' => $this->appid,
            'aNumber' => $phoneA,
            'bNumber' => $phoneB,
            'xNumberRestrict' => '0',
            'mappingDuration' => $mapTime,
            'needRecord' => '1',
//            'cdrNotifyUrl'=>'',
        ];
//        $request = new Request($mothed, $baseUrl, [
//            'headers' => [
//                'Accept' => 'application/json',
//                'Content-Type'     => 'application/json',
//                'Authorization'      => $auth
//            ],
//            'json' => $data,
//            'debug' => true,
//        ]);


        $info = [
            'anumber' => $phoneA,
            'bnumber' => $phoneB,
            'xnumber' => '',
            'timestamp' => $timestamp,
            'response' => '',
            'status' => 1,
        ];
        $response = $client->request($mothed, $baseUrl, [
            'headers' => [
                'Accept' => 'application/json',
                'Content-Type' => 'application/json;charset=utf-8',
                'Authorization' => $auth,
            ],
            'json' => $data,
//            'debug' => true,
        ]);
//        $response = $client->send($request);

        $ret = $response->getBody()->getContents();
        $info['response'] = $ret;
        $ret = json_decode($ret, true);
        dump($ret);
        $statusCode = 1;
        $msg = '申请电话隐私保护成功!';
        $rdata='';
        if ($ret['statusCode'] != '000000') {//成功
            $info['status'] = 0;
            $statusCode = 0;
            $msg = '申请电话隐私保护失败，请联系后台管理员!';

        }
        else{
            $rdata=$ret['data']['xNumber'];
            $info['xnumber'] = $rdata;
        }
        Phonemap::create($info);
        return $this->writeJson($statusCode, $rdata, $msg);

    }

    public function sendSMS($phone='17783040810',$templateId=1,$param=''){
        if (empty($phone)||empty($templateId)) return response()->json(["code"=>0, "msg"=>'参数错误']);

        $client = new Client();
        $mothed = 'POST';
        $timestamp = time();
        $param=array('1222','10');
//        【云通讯】您使用的是云通讯短信模板，您的验证码是{1}，请于{2}分钟内正确输入。

        $auth = "$this->accountSid:" . date("YmdHis", $timestamp);
//        dump('Authorization'.$auth);
        $auth = base64_encode($auth);
        $sigParameter = $this->accountSid . $this->key . date("YmdHis", $timestamp);
//        dump('sigParameter'.$sigParameter);
        $sigParameter = md5($sigParameter);
        $baseUrl = $this->baseurl . "/Accounts/$this->accountSid/SMS/TemplateSMS?sig=$sigParameter";
        $data = [
            'appId' => $this->appid,
            'to' => $phone,
            'templateId' => $templateId,
            'datas' => $param,
//            'cdrNotifyUrl'=>'',
        ];
        $response = $client->request($mothed, $baseUrl, [
            'headers' => [
                'Accept' => 'application/json',
                'Content-Length'=> 256,
                'Content-Type' => 'application/json;charset=utf-8',
                'Authorization' => $auth,
            ],
            'json' => $data,
//            'debug' => true,
        ]);
        $ret = $response->getBody()->getContents();
        $info['response'] = $ret;
        $ret = json_decode($ret, true);
        dump($ret);
    }
}